#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 2e3 + 10;

int T , n;
ll a[MAXN];
ll dp[MAXN][MAXN];

int main()
{
    freopen("color.in" , "r" , stdin);
    freopen("color.out" , "w" , stdout);
    scanf("%d" , &T);
    while (T --)
    {
        memset(dp , 0 , sizeof(dp));
        scanf("%d" , &n);
        for(int i = 1 ; i <= n ; i ++)
            scanf("%lld" , a + i);
        for(int close_0 = 0 ; close_0 < n ; close_0 ++)
            for(int close_1 = 0 ; close_1 < n ; close_1 ++)
                if(a[close_0] == a[n] || a[close_1] == a[n])
                    dp[close_0][close_1] = a[n];
        for(int p = n - 1 ; p >= 1 ; p --)
        {
            for(int close_0 = p - 1 ; close_0 >= 0 ; close_0 --)
            {
                for(int close_1 = p - 1 ; close_1 >= 0 ; close_1 --)
                {
                    if(a[close_0] == a[p] && a[close_1] != a[p])
                        dp[close_0][close_1] = max(dp[p][close_1] + a[p] , dp[close_0][p]);
                    else if(a[close_0] != a[p] && a[close_1] == a[p])
                        dp[close_0][close_1] = max(dp[close_0][p] + a[p] , dp[p][close_1]);
                    else if(a[close_0] == a[p] && a[close_1] == a[p])
                        dp[close_0][close_1] = max(dp[close_0][p] + a[p] , dp[p][close_1] + a[p]);
                    else
                        dp[close_0][close_1] = max(dp[close_0][p] , dp[p][close_1]);
                }
            }
        }
        // for(int i = 1 ; i <= n ; i ++)
        //     cout << dp[i][1][1] << ' ';
        // cout << endl;
        printf("%lld\n" , dp[0][0]);
    }
    
    return 0;
}